package com.xuecheng.manage_course.service;

import com.xuecheng.framework.domain.cms.CmsPage;
import com.xuecheng.framework.domain.cms.response.CmsPageResult;
import com.xuecheng.framework.domain.course.CourseBase;
import com.xuecheng.framework.domain.course.CourseMarket;
import com.xuecheng.framework.domain.course.CoursePic;
import com.xuecheng.framework.domain.course.ext.CourseView;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.domain.course.response.CourseCode;
import com.xuecheng.framework.domain.course.response.CoursePublishResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.manage_course.dao.CourseBaseRepository;
import com.xuecheng.manage_course.dao.CourseMarketRepository;
import com.xuecheng.manage_course.dao.CoursePicRepository;
import com.xuecheng.manage_course.dao.TeachplanMapper;
import com.xuecheng.manage_course.feign.cms.CmsPageFeign;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Optional;

@Service
public class CourseViewService {
    @Resource
    private CourseBaseRepository courseBaseRepository;
    @Resource
    private CoursePicRepository coursePicRepository;
    @Resource
    private CourseMarketRepository courseMarketRepository;
    @Resource
    private TeachplanMapper teachplanMapper;
    @Resource
    private CmsPageFeign cmsPageFeign;
    @Value("${course-publish.siteId}")
    private String publish_siteId;
    @Value("${course-publish.templateId}")
    private String publish_templateId;
    @Value("${course-publish.previewUrl}")
    private String publish_previewUrl;
    @Value("${course-publish.pageWebPath}")
    private String publish_pageWebPath;
    @Value("${course-publish.pagePhysicalPath}")
    private String publish_pagePhysicalPath;
    @Value("${course-publish.dataUrl}")
    private String publish_dataUrl;
    /**
     * 门户课程页详细信息查询,包括课程信息,课程图片,营销计划,课程计划
     * @param id 课程id
     * @return 封装好的实体类
     */
    public CourseView getCourseView(String id){
        CourseView courseView = new CourseView();
        // 查询课程的基本信息
        Optional<CourseBase> optional = courseBaseRepository.findById(id);
        if (optional.isPresent()){
            courseView.setCourseBase(optional.get());
            // 查询课程图片
            Optional<CoursePic> optionalCoursePic = coursePicRepository.findById(id);
            optionalCoursePic.ifPresent(courseView::setCoursePic);
            // 查询营销信息
            Optional<CourseMarket> optionalCourseMarket = courseMarketRepository.findById(id);
            optionalCourseMarket.ifPresent(courseView::setCourseMarket);
            // 课程计划信息
            TeachplanNode teachplanNode = teachplanMapper.selectList(id);
            courseView.setTeachplanNode(teachplanNode);
            return courseView;
        }else {
            return null;
        }
    }

    /**
     * 课程预览接口
     *
     * @param id 课程id
     * @return 远程调用cmd获取到的课程预览url
     */
    public CoursePublishResult preview(String id) {
        Optional<CourseBase> optional = courseBaseRepository.findById(id);
        if (!optional.isPresent()){
            ExceptionCast.cast(CourseCode.COURSE_DOES_NOT_EXIST);
        }
        // 请求Cms添加页面
        // 准备CmsPage信息
        CmsPage cmsPage = new CmsPage();
        cmsPage.setSiteId(publish_siteId); // 站点id
        cmsPage.setDataUrl(publish_dataUrl+id); //数据url
        cmsPage.setPageName(id + ".html"); //课程静态文件名
        cmsPage.setPageAliase(optional.get().getName()); //静态页面名称,就是课程名
        cmsPage.setPageWebPath(publish_pageWebPath); //Web访问路径
        cmsPage.setPagePhysicalPath(publish_pagePhysicalPath); // 页面的物理路径
        cmsPage.setTemplateId(publish_templateId); //设置模板id
        // 远程调用
        CmsPageResult cmsPageResult = cmsPageFeign.saveCmsPage(cmsPage);
        if (!cmsPageResult.isSuccess()){
            return new CoursePublishResult(CommonCode.FAIL,null);
        }
        CmsPage one = cmsPageResult.getCmsPage();
        String pageId = one.getPageId();
        // 拼装页面预览的url
        String previewUrl = publish_previewUrl + pageId;
        // 返回CoursePublishResult  CoursePublishResult中有页面预览的url
        return new CoursePublishResult(CommonCode.SUCCESS,previewUrl);
    }
}
